<?php


namespace Admin\Model;
use Think\Model;

class GoodsTypeModel extends Model {

//添加分类
    public function toadd($data = null){

        $data = $this->create($data);
//        dump($data);
        if(empty($data)){
            return false;
        }

        $z = $this->add();
        if(!$z){
//            $this->error = "新增出错";
            return false;
        }
        $data2 = $this->field('pathid,level')->find($data['pid']);

        $a = $data2['pathid'].','.$z;
        $b = $data2['level']+1;

        $result = $this->where("id = $z")->setField(array('pathid'=>$a,'level'=>$b));

        return $result;
    }
//修改分类
    public function upd($data = null){

        $data2 = $this->field('pathid,level')->find($data['pid']);

        $data['pathid'] = $data2['pathid'].','.$data['id'];
        $data['level'] = $data2['level']+1;

        $data = $this->create($data);
        if(empty($data)){
            return false;
        }
        $z = $this->save();
//        dump($z);
        if(!$z){
//            $this->error = "更新出错";
            return false;
        }

        return $data;
    }
//获取分类列表支持分页
//获取一级分类列表支持分页
    public function lists($category, $order = '`id` DESC', $status = 1, $field = true, $limit = '10', $map = array()){
        $map = array_merge($this->listMap($category, $status), $map);
        return $this->field($field)->where($map)->order($order)->limit($limit)->select();
    }
//获取当前分类的下级分类
//获取当前分类的所有下级分类
    public function typedown($id = 0){

        $info = $this->where("pid = $id")->select();

        return $info;

    }
//获取当前分类的上级分类
//获取当前分类的上级所有分类
    public function typeup($pid = 0){

        $info = $this->find($pid);

        return $info;
    }
//批量修改排序、状态
    public function state($id,$state){

        if(is_array($id)){
            if (empty($id)) {
                return false;
            }
            $id = array_unique((array)$id);
//            $id = is_array($id) ? implode(',', $id) : $id;
            $map = array('id' => array('in', $id) );

            $z = $this->where($map)->setField('state',$state);
        }else{
            if($state){
                $z = $this->where("id = $id")->setField('state','0');
            }else{
                $z = $this->where("id = $id")->setField('state','1');
            }
        }

        return $z;

    }
//批量修改分类归属
    public function type($id = null){

    }
//删除制定分类或批量删除
    public function del($id = null){
//dump($id);
        if (empty($id)) {
            return false;
        }

        $id = array_unique((array)$id);
//            $id = is_array($id) ? implode(',', $id) : $id;
        $map = array('id' => array('in', $id) );

        $z = $this->where($map)->setField('state','-1');

        if ($z) {
            return true;
//            $this->success('删除成功', U('index'));
        } else {
            return false;
//            $this->success('删除失败', U('index'));
        }
    }



}
